package com.ruoyi.food.service.impl;

import cn.hutool.core.util.IdUtil;
import com.ruoyi.common.core.utils.DateUtils;
import com.ruoyi.common.core.utils.SecurityUtils;
import com.ruoyi.environment.domain.OpmEnvRule;
import com.ruoyi.food.domain.OpmFoodSafeCheck;
import com.ruoyi.food.mapper.OpmFoodSafeCheckMapper;
import com.ruoyi.food.service.IOpmFoodSafeCheckService;
import com.ruoyi.system.api.LinkFileServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 食品检查Service业务层处理
 *
 * @author cjie
 * @date 2021-07-24
 */
@Service
public class OpmFoodSafeCheckServiceImpl extends LinkFileServiceImpl implements IOpmFoodSafeCheckService {
    @Autowired
    private OpmFoodSafeCheckMapper opmFoodSafeCheckMapper;

    /**
     * 查询食品检查
     *
     * @param id 食品检查ID
     * @return 食品检查
     */
    @Override
    public OpmFoodSafeCheck selectOpmFoodSafeCheckById(Long id) {
        OpmFoodSafeCheck result = opmFoodSafeCheckMapper.selectOpmFoodSafeCheckById(id);
        result.setFileList(getFileList(id.toString()));
        return result;
    }

    /**
     * 查询食品检查列表
     *
     * @param opmFoodSafeCheck 食品检查
     * @return 食品检查
     */
    @Override
    public List<OpmFoodSafeCheck> selectOpmFoodSafeCheckList(OpmFoodSafeCheck opmFoodSafeCheck) {
        return opmFoodSafeCheckMapper.selectOpmFoodSafeCheckList(opmFoodSafeCheck);
    }

    /**
     * 新增食品检查
     *
     * @param opmFoodSafeCheck 食品检查
     * @return 结果
     */
    @Override
    public int insertOpmFoodSafeCheck(OpmFoodSafeCheck opmFoodSafeCheck) {
        opmFoodSafeCheck.setId(IdUtil.getSnowflake(0, 0).nextId());
        opmFoodSafeCheck.setCreateTime(DateUtils.getNowDate());
        opmFoodSafeCheck.setCreateBy(SecurityUtils.getUsername());
        linkFile(opmFoodSafeCheck.getFileIdList(), opmFoodSafeCheck.getId().toString());
        return opmFoodSafeCheckMapper.insertOpmFoodSafeCheck(opmFoodSafeCheck);
    }

    /**
     * 修改食品检查
     *
     * @param opmFoodSafeCheck 食品检查
     * @return 结果
     */
    @Override
    public int updateOpmFoodSafeCheck(OpmFoodSafeCheck opmFoodSafeCheck) {
        opmFoodSafeCheck.setUpdateTime(DateUtils.getNowDate());
        opmFoodSafeCheck.setUpdateBy(SecurityUtils.getUsername());
        linkFile(opmFoodSafeCheck.getFileIdList(), opmFoodSafeCheck.getId().toString());
        return opmFoodSafeCheckMapper.updateOpmFoodSafeCheck(opmFoodSafeCheck);
    }

    /**
     * 批量删除食品检查
     *
     * @param ids 需要删除的食品检查ID
     * @return 结果
     */
    @Override
    public int deleteOpmFoodSafeCheckByIds(Long[] ids) {
        String[] res = new String[ids.length];
        for(int i=0;i<ids.length;i++){
            res[i] = String.valueOf(ids[i]);
        }
        deleteLinkFile(res);
        return opmFoodSafeCheckMapper.deleteOpmFoodSafeCheckByIds(ids);
    }

    /**
     * 删除食品检查信息
     *
     * @param id 食品检查ID
     * @return 结果
     */
    @Override
    public int deleteOpmFoodSafeCheckById(Long id) {
        return opmFoodSafeCheckMapper.deleteOpmFoodSafeCheckById(id);
    }
}
